package com.personal.cuncunxing.module.home.live;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.personal.cuncunxing.R;
import com.personal.cuncunxing.module.home.pojo.Info;

/**
 * Description:
 * Created by kode on 2020/5/27.
 */
public class LiveSubRVAdapter extends RecyclerView.Adapter<LiveSubRVAdapter.ViewHolder> {

    private static final String TAG = "LiveSubRVAdapter";
    private LiveRVAdapter liveRVAdapter;
    private int sortPosition;//标记当前的类别位置
    private int lastPosition;//标记上一次按下的位置
    private OnTvPlay onTvPlay;
    private Context context;

    LiveSubRVAdapter(int position, LiveRVAdapter liveRVAdapter) {
        this.sortPosition = position;
        this.liveRVAdapter = liveRVAdapter;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_live_sub_item, parent, false);
        context = parent.getContext();
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        try {
            holder.live_sub_title_tv.setText(Info.getInstance().getInfo2tvList().get(sortPosition).getTv().get(position).getT_name());
            holder.play_item.setBackground(context.getDrawable(R.drawable.home_live_sub_card));
            holder.selected_iv.setVisibility(View.INVISIBLE);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "onBindViewHolder: ");
        holder.play_item.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                onTvPlay.OnPlay(Info.getInstance().getInfo2tvList().get(sortPosition).getTv().get(position).getT_link()[0]);
                holder.play_item.setBackground(context.getDrawable(R.drawable.home_live_sub_card_selected));
                holder.selected_iv.setVisibility(View.VISIBLE);
                updateLastItemUI(position);
            }
        });
    }

    public void updateLastItemUI(int position) {
        /**
         * 难点二：已选择item更新相应的ui,同时刷新上一次按下的itemUI
         * 一开始将标记上一次的类别位置放置在子类适配器，但是每次更新该适配器时，该标记都重新置为0，
         * 所以将它移动到LiveRVAdapter中，便可以解决问题。
         */

        if (lastPosition != position) {//刷新上一个被点击过的itemUI
            notifyItemChanged(lastPosition);
        }
        lastPosition = position;
        if (liveRVAdapter.getLastSortPosition() != sortPosition) {//刷新上一次的类别位置
            liveRVAdapter.updateLastItemUI(liveRVAdapter.getLastSortPosition());
        }
        liveRVAdapter.setLastSortPosition(sortPosition);
    }

    @Override
    public int getItemCount() {
        try {
            if (Info.getInstance().getInfo2tvList() != null)
                return Info.getInstance().getInfo2tvList().get(sortPosition).getTv().size();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return 6;
    }

    public void setOnTvPlay(OnTvPlay onTvPlay) {
        this.onTvPlay = onTvPlay;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView live_sub_title_tv;
        RelativeLayout play_item;
        ImageView selected_iv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            live_sub_title_tv = itemView.findViewById(R.id.live_sub_title_tv);
            play_item = itemView.findViewById(R.id.play_item);
            selected_iv = itemView.findViewById(R.id.selected_iv);
        }
    }

    interface OnTvPlay {
        void OnPlay(String url);
    }
}
